package com.fg.cloud.manager.service.delivery.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.BillNoUtils;
import com.fg.cloud.common.ServiceException;
import com.fg.cloud.common.dictionary.DicUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.delivery.DeliveryDetailEntity;
import com.fg.cloud.manager.entry.delivery.DeliveryInfoEntity;
import com.fg.cloud.manager.entry.order.OrderDetailEntity;
import com.fg.cloud.manager.entry.order.OrderInfoEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.delivery.DeliveryInfoMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.delivery.DeliveryDetailService;
import com.fg.cloud.manager.service.delivery.DeliveryInfoService;
import com.fg.cloud.manager.service.order.OrderDetailService;

/**
 * @author ：
 * @date ： 2017年6月27日 上午8:50:50
 * @Desc ：发货单
 */
@Service
public class DeliveryInfoServiceImpl extends BaseServiceImpl implements DeliveryInfoService {

	@Autowired
	private DeliveryInfoMapper deliveryInfoMapper;
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private DeliveryDetailService deliveryDetailService;

	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return deliveryInfoMapper;
	}

	// 生成发货单
	@Override
	@Transactional
	public int delivery(OrderInfoEntity oi, DeliveryInfoEntity deliveryInfoEntity) {
		int temp = 0;
		try {
			List<OrderDetailEntity> listods = new ArrayList<>();
			Map<String, Object> param = new HashMap<>();
			param.put("orderId", oi.getId());
			listods = orderDetailService.select(param);

			// 1、生成发货单
			deliveryInfoEntity.setDeliveryStatus(DicUtils.ORDERSTATUS_NoRECEIVE);
			String deliveryNo = DicUtils.DELIVERYNO + BillNoUtils.getBillNo();
			deliveryInfoEntity.setDeliveryNo(deliveryNo);
			int deliveryId = deliveryInfoMapper.insert(deliveryInfoEntity);
			if (deliveryId <= 0) {
				return -1;
			}

			for (OrderDetailEntity od : listods) {
				for (DeliveryDetailEntity dd : deliveryInfoEntity.getListDetails()) {
					if (od.getProductId() == dd.getProductId()) {
						if (dd.getAmount().add(od.getDeliveryAmount()).compareTo(od.getAmount()) == -1) { // 发货数量少于订单数量
							temp = DicUtils.ORDERSTATUS_DELIVER_PART;
						}
						//2、 更改订单明细发货数量
						od.setDeliveryAmount(dd.getAmount().add(od.getDeliveryAmount()));
						orderDetailService.update(od);//最好单独写个防并发的方法，时间关系就先这样。

						//3、 生成发货单明细
						dd.setDeliveryId((long)deliveryId);
						deliveryDetailService.insert(dd);
					}
				}
			}
		} catch (ServiceException se) {
			se.printStackTrace();
			return -1;
		}

		return temp;
	}

	/**
	 * 根据facId 删除发货单相关记录
	 */
	@Override
	@Transactional
	public boolean deleteDelivery(Long facId) {
		//1.删除发货单详情
		boolean r1 = deliveryDetailService.delByFacId(facId);
		if ( !r1 ) return false;
		//2.删除发货单日志
		Long r2 = deliveryInfoMapper.delDeliveryLog(facId);
		if ( r2<=0 ) return false;
		//3.删除发货单附件
		Long r3 = deliveryInfoMapper.delDeliveryFile(facId);
		if ( r3<=0 ) return false;
		//4.删除发货单
		Long r4 = deliveryInfoMapper.delDeliveryInfo(facId);
		
		if ( r1 && r2 > 0 && r3 > 0 && r4 > 0 ) {
			return true;
		}
		
		return false;
	}

}
